import torch
import numpy as np
import re

# data
ff = open("housing.data").readlines()
data = []
for item in ff:
    out = re.sub(r"\s{2,}", " ", item).strip()
    # print(out)
    data.append(out.split(" "))

data = np.array(data).astype(np.float32)

y = data[:, -1]
x = data[:, 0:-1]

x_train = x[0:496, ...]
y_train = y[0:496, ...]

x_test = x[496:, ...]
y_test = y[496:, ...]


# net
class Net(torch.nn.Module):
    def __init__(self, n_feature, n_output):
        super(Net, self).__init__()

        self.hidden = torch.nn.Linear(n_feature, 100)
        self.predict = torch.nn.Linear(100, n_output)

    def forward(self, x):
        out = self.hidden(x)
        out = torch.relu(out)
        out = self.predict(out)
        return out


net = torch.load("model/model.pkl")

# loss
loss_func = torch.nn.MSELoss()

x_data = torch.tensor(x_test, dtype=torch.float32)
y_data = torch.tensor(y_test, dtype=torch.float32)

pred = net.forward(x_data)
pred = torch.squeeze(pred)
loss = loss_func(pred, y_data) * 0.001
print("loss_test:{}".format(loss))
